const WONDERS_PER_PAGE = 'WONDERS_PER_PAGE';
// 新增跳转的当前页
const WONDERS_CURRENT_PAGE = 'WONDERS_CURRENT_PAGE';
const WONDERS_DiagnosisList_PAGE = 'WONDERS_DiagnosisList_PAGE';

/*eslint spaced-comment:0*/
/*eslint import/prefer-default-export:0*/
export const dateFormat = function(ts) {
  const d = new Date(ts);
  const yyyy = `0000${d.getFullYear()}`.slice(-4);
  const MM = `00${d.getMonth() + 1}`.slice(-2);
  const dd = `00${d.getDate()}`.slice(-2);
  const HH = `00${d.getHours()}`.slice(-2);
  const mm = `00${d.getMinutes()}`.slice(-2);
  const ss = `00${d.getSeconds()}`.slice(-2);
  return `${yyyy}-${MM}-${dd} ${HH}:${mm}:${ss}`;
};

const _sessionStorage = {
  get: function(key) {
    const data = sessionStorage.getItem(key);
    if (!data || data === 'null') {
      return null;
    }
    return JSON.parse(data);
  },
  set: function(key, value) {
    sessionStorage.setItem(key, JSON.stringify(value));
  },
  remove: function(key) {
    sessionStorage.removeItem(key);
  }
};

const isPromptLoginMessage = function(message, tag, millisecond = 1000) {
  const time = _sessionStorage.get(`${message}${tag}`);
  const nowTimestamp = new Date().getTime();
  _sessionStorage.set(`${message}${tag}`, { timestamp: nowTimestamp });
  if (time) {
    const timestamp = time['timestamp'];
    return timestamp + millisecond < nowTimestamp;
  } else {
    return true;
  }
};

// table页面每页条数设置获取
const itemsPerPage = {
  get: function() {
    const state = JSON.parse(sessionStorage.getItem('state'));
    const count = sessionStorage.getItem(
      `${WONDERS_PER_PAGE}.${state.user.name}`
    );
    return JSON.parse(count) || 20;
  },
  set: function(count) {
    const state = JSON.parse(sessionStorage.getItem('state'));
    sessionStorage.setItem(`${WONDERS_PER_PAGE}.${state.user.name}`, count);
  }
};

// table综合查询页面当前页数设置获取
const itemsCurrentPage = {
  get: function() {
    const state = JSON.parse(sessionStorage.getItem('state'));
    const count = sessionStorage.getItem(
      `${WONDERS_CURRENT_PAGE}.${state.user.name}`
    );
    return JSON.parse(count) || 1;
  },
  set: function(count) {
    const state = JSON.parse(sessionStorage.getItem('state'));
    sessionStorage.setItem(`${WONDERS_CURRENT_PAGE}.${state.user.name}`, count);
  }
};
// table院内诊断页面当前页数设置获取
const itemsDiagnosisListPage = {
  get: function() {
    const state = JSON.parse(sessionStorage.getItem('state'));
    const count = sessionStorage.getItem(
      `${WONDERS_DiagnosisList_PAGE}.${state.user.name}`
    );
    return JSON.parse(count) || 1;
  },
  set: function(count) {
    const state = JSON.parse(sessionStorage.getItem('state'));
    sessionStorage.setItem(
      `${WONDERS_DiagnosisList_PAGE}.${state.user.name}`,
      count
    );
  }
};

/**
 * 深度克隆
 * @param data 数据
 * @returns {*}
 */
const deepClone = function(data) {
  if (data === undefined || data === null) {
    return data;
  }
  return JSON.parse(JSON.stringify(data || {}));
};

export function os() {
  var ua = navigator.userAgent,
    isWindowsPhone = /(?:Windows Phone)/.test(ua),
    isSymbian = /(?:SymbianOS)/.test(ua) || isWindowsPhone,
    isAndroid = /(?:Android)/.test(ua),
    isFireFox = /(?:Firefox)/.test(ua),
    isChrome = /(?:Chrome|CriOS)/.test(ua),
    isTablet =
      /(?:iPad|PlayBook)/.test(ua) ||
      (isAndroid && !/(?:Mobile)/.test(ua)) ||
      (isFireFox && /(?:Tablet)/.test(ua)),
    isPhone = /(?:iPhone)/.test(ua) && !isTablet,
    isPc = !isPhone && !isAndroid && !isSymbian;
  return {
    isTablet: isTablet,
    isPhone: isPhone,
    isAndroid: isAndroid,
    isPc: isPc
  };
}

export {
  isPromptLoginMessage,
  itemsPerPage,
  itemsCurrentPage,
  itemsDiagnosisListPage,
  deepClone
};
